from django.shortcuts import render
from django.shortcuts import render
from django.http import Http404, HttpResponse, HttpResponseRedirect
from django.http import JsonResponse
from django.urls import reverse

import requests

from apps.spider.models import Task
from lib.scrapyd_api import ScrapydClient
from lib.schd import get_scheduler


# Create your views here.
def run_once_job(request, tid):
    task = Task.objects.get(task_id=tid)
    spider = task.spider

    # post数据1
    project = spider.project_name  # scrapy项目名称
    spider_name = spider.spider_name  # scrapy爬虫名称
    data = [("project", project), ("spider", spider_name)]
    spider_setting = task.setting.strip().split()
    settings = [("setting", v) for v in spider_setting]
    data.extend(settings)
    # post数据2(spider实例化参数)
    spider_args = task.spider_args

    spdcli = ScrapydClient.get_cli(spider.ip, spider.port)
    d = spdcli.schedule_scrapy(data)
    return JsonResponse({"status":d["status"]})


def add_job(request, tid):
    """向scrapyd服务发送运行请求
    """
    task = Task.objects.get(task_id=tid)
    spider = task.spider

    # post数据1
    project = spider.project_name  # scrapy项目名称
    spider_name = spider.spider_name  # scrapy爬虫名称
    data = [("project", project), ("spider", spider_name)]
    spider_setting = task.setting.strip().split()
    settings = [("setting", v) for v in spider_setting]
    data.extend(settings)
    # post数据2(spider实例化参数)
    spider_args = task.spider_args

    # 任务定时参数
    every = task.every
    period = task.period
    tragger_args = {period:every}

    spdcli = ScrapydClient.get_cli(spider.ip, spider.port)
    schd = get_scheduler()
    schd.add_job(func=spdcli.schedule_scrapy,
                 trigger="interval",
                 id=str(tid),
                 kwargs={"post_data":data},
                 **tragger_args,
                )
    return JsonResponse({"status":"ok!!"})
    

def remove_job(request, tid):
    schd = get_scheduler()
    schd.remove_job(tid)
    return JsonResponse({"status":"ok!!"})


def pause_job(request, tid):
    """任务中断
    """
    schd = get_scheduler()
    schd.pause_job(tid)
    return JsonResponse({"status":"ok!!"})


def resume_job(request, tid):
    """恢复中断任务
    """
    schd = get_scheduler()
    schd.resume_job(tid)
    return JsonResponse({"status":"ok!!"})


def modify_job(request, tid):
    schd = get_scheduler()
    job = schd.get_job(tid)
    job.modify()  #example
    return JsonResponse({"status":"ok!!"})


def reschedule_job(request, tid):
    schd = get_scheduler()
    schd.reschedule_job(tid, minute=5)  # example
    return JsonResponse({"status":"ok!!"})


def check_job_status(request, tid):
    schd = get_scheduler()
    job = schd.get_job(tid)
    if not job:
        return JsonResponse({"status_code": -1, "status":"not exist!!!"})  # 不存在

    next_run_time = job.next_run_time
    if not next_run_time:
        return JsonResponse({"status_code": 0, "status":"pause!"})  # 中断
    else:
        return JsonResponse({"status_code": 1, "status":"running..."})  # 正在运行


def pause_schedule(request):
    schd = get_scheduler()
    schd.pause()
    return JsonResponse({"status":"ok!!"})


def resume_schedule(request):
    schd = get_scheduler()
    schd.resume()
    #schd.start()
    return JsonResponse({"status":"ok!!"})


